package com.guoranhao.web.resolver;

import com.guoranhao.tool.utils.LogUtil;
import com.guoranhao.tool.vo.MessageVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * Description: controller全局异常处理器
 * Copyright (c) Department of Research and Development/Beijing
 * All Rights Reserved.
 *
 * @version 1.0  2017/9/14 15:24 by 郝浩天 （haohaotian@cloud-young.com/）创建
 */
public class SpringExceptionHandler {

    private static final Logger logger = LoggerFactory.getLogger(SpringExceptionHandler.class);

    @ExceptionHandler(Exception.class)
    public @ResponseBody
    MessageVo handleAllException(Exception e){
        RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
        HttpServletRequest request = ((ServletRequestAttributes)requestAttributes).getRequest();
        LogUtil.error(logger,e,"ExceptionHandler,Http Request URI:{0}",request.getRequestURI());
        return new MessageVo(false,"-1","网络异常");
    }
}
